package com.frxuexi.mynews.adapter

import android.util.Log
import android.util.TimeUtils
import android.widget.TimePicker
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.frxuexi.mynews.R
import com.frxuexi.mynews.entity.CommentEntity
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale


class CommentAdapter(layout:Int):BaseQuickAdapter<CommentEntity,BaseViewHolder>(layout) {
    override fun convert(helper: BaseViewHolder, item: CommentEntity) {
        helper.apply {
            setText(R.id.com_content,item.content)


            val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
            //评论时间
            val createTime = format.parse(item.createTime).time

            //系统当前时间
            val currentTime = Date().time
            //计算时间差
            var time=currentTime-createTime

            Log.i("===time",time.toString())

            //获取分钟
            var minute=time/1000/60
            //获取小时
            var hour=minute/60
            //获取天数
            var day=hour/24

            //显示具体日期，还是几天前，还是几小时前，还是几分钟前
            Log.i("===time",time.toString())

            //获取当前年份
            val year = Calendar.getInstance().get(Calendar.YEAR)

            if(day>=7){//超过7天，显示具体日期
                //如果评论时间是今年
                if(item.createTime.substring(0,4)==year.toString()){
                    //只显示月日
                    setText(R.id.com_time, item.createTime.substring(5, 10))
                }else{
                    //否则显示年月日
                    setText(R.id.com_time, item.createTime.substring(0, 10))
                }
            }else if(day<7 && hour>23){
                setText(R.id.com_time,"${day+1}天前")
            }else if(hour<=23 && minute>59){
                setText(R.id.com_time,"${hour+1}小时前")
            }else if(minute<=59 && minute>=1){
                setText(R.id.com_time,"${minute+1}分钟前")
            }else if(minute<1){
                setText(R.id.com_time,"刚刚")
            }
        }
    }
}